package service

import (
	"github.com/astaxie/beego/orm"
	"net/url"
	"strings"
	"xianzhi/models"
	"xianzhi/utils"
)

// 获取订单学校信息
func GetOrderSchoolInfo(p map[string]interface{}) *models.OrderSchool {
	o := orm.NewOrm()
	orderSchool := &models.OrderSchool{}
	qs := o.QueryTable("OrderSchool")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["name"]; ok {
		qs = qs.Filter("name", p["name"])
	}
	if _, ok := p["status"]; ok {
		qs = qs.Filter("status", p["status"])
	}
	qs.One(orderSchool)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if orderSchool.Id > 0 {
			orderSchool.SchoolIsExcellentShow = models.SchoolIsExcellent[orderSchool.SchoolIsExcellent]
			orderSchool.SchoolIsAssistShow = models.SchoolIsAssist[orderSchool.SchoolIsAssist]
		}
	}
	return orderSchool
}

// 添加单个订单学校
func AddOrderSchool(input url.Values) (int64, error) {
	o := orm.NewOrm()
	orderSchool := &models.OrderSchool{}
	order := &models.Order{}
	if _, ok := input["order_id"]; ok {
		order.Id = utils.Atoi64(input["order_id"][0])
		o.Read(order)
	}
	orderSchool.Order = order
	school := &models.School{}
	if _, ok := input["school_id"]; ok {
		school.Id = utils.Atoi64(input["school_id"][0])
		o.Read(school)
	}
	orderSchool.School = school
	if school.Id > 0 {
		orderSchool.SchoolName = school.Name
		orderSchool.SchoolIntroduce = school.Introduce
		orderSchool.SchoolIsExcellent = school.IsExcellent
		orderSchool.SchoolIsAssist = school.IsAssist
	}
	return o.Insert(orderSchool)
}

// 添加多个订单学校
func AddOrderSchools(orderId int64, schoolIds string) (int64, error) {
	o := orm.NewOrm()
	order := &models.Order{}
	order.Id = orderId
	o.Read(order)
	var orderSchools []models.OrderSchool
	schoolIdArr := strings.Split(schoolIds, ",")
	for _, schoolId := range schoolIdArr {
		school := models.School{}
		school.Id = utils.Atoi64(schoolId)
		o.Read(&school)
		orderSchool := models.OrderSchool{}
		orderSchool.Order = order
		orderSchool.School = &school
		orderSchool.SchoolName = school.Name
		orderSchool.SchoolIntroduce = school.Introduce
		orderSchool.SchoolIsExcellent = school.IsExcellent
		orderSchool.SchoolIsAssist = school.IsAssist
		orderSchools = append(orderSchools, orderSchool)
	}
	return o.InsertMulti(1, orderSchools)
}

// 修改订单学校信息
func EditOrderSchool(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("OrderSchool").Filter("id", input["id"][0]).Update(p)
}

// 获取订单学校列表
func GetOrderSchoolList(p map[string]interface{}) (int64, []*models.OrderSchool) {
	var orderSchoolList []*models.OrderSchool
	o := orm.NewOrm()
	qs := o.QueryTable("OrderSchool")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["orderId"]; ok {
		qs = qs.Filter("order_id", p["orderId"])
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("order__user__Id", p["userId"])
	}
	if _, ok := p["cityId"]; ok {
		qs = qs.Filter("school__city__id", p["cityId"])
	}
	if _, ok := p["name"]; ok {
		qs = qs.Filter("school_name__contains", p["name"])
	}
	if _, ok := p["isExcellent"]; ok {
		qs = qs.Filter("school_is_excellent", p["isExcellent"])
	}
	if _, ok := p["isAssist"]; ok {
		qs = qs.Filter("school_is_assist", p["isAssist"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&orderSchoolList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, orderSchool := range orderSchoolList {
			orderSchool.SchoolIsExcellentShow = models.SchoolIsExcellent[orderSchool.SchoolIsExcellent]
			orderSchool.SchoolIsAssistShow = models.SchoolIsAssist[orderSchool.SchoolIsAssist]
		}
	}
	return totalCount, orderSchoolList
}

func GetSchoolIds(p map[string]interface{}) (orm.ParamsList, error) {
	var ids orm.ParamsList
	o := orm.NewOrm()
	qs := o.QueryTable("OrderSchool")
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("order__user__id", p["userId"])
	}
	_, err := qs.ValuesFlat(&ids, "school_id")
	if err != nil {
		return nil, err
	}
	return ids, err
}

func DelOrderSchool(p map[string]interface{}) (int64, error) {
	if len(p) < 1 {
		return -1, nil
	}
	o := orm.NewOrm()
	qs := o.QueryTable("OrderSchool")
	if _, ok := p["orderId"]; ok {
		qs = qs.Filter("order_id", p["orderId"])
	}
	return qs.Delete()
}
